﻿using Microsoft.SqlServer.Server;
using NPOI.SS.Formula.Functions;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace HEFramework
{
    internal enum PropertySetting
    {
        different = 1,
        outputKey,
    }

    internal abstract class Property
    {
        private HashSet<string> differentContents;
        public PropertySetting[] PropertySettings { get; private set; }
        public bool IsArray { get; private set; }

        public string Setting { get; private set; }
        public string Name { get; private set; }
        public string Type { get; private set; }
        public string Note { get; private set; }
        public int Column { get; private set; }


        public bool IsDifferent => HasSetting(PropertySetting.different);
        public bool IsOutputKey => HasSetting(PropertySetting.outputKey);


        public DataTableSettingScriptableObject data = DataTableSettingScriptableObject.Instance;

        public void Init(string _name, string _type, string _note, string _setting, int _column, PropertySetting[] _propertySetting, bool _isArray)
        {
            Setting = _setting;
            Name = _name;
            Type = _type;
            Note = _note;
            Column = _column;

            IsArray = _isArray;
            PropertySettings = _propertySetting;
            if (IsDifferent)
            {
                differentContents = new HashSet<string>();
            }
        }

        public bool ChangeSheet(string _name, string _type, string _note, string _setting, int _column)
        {
            if (Name == _name || Type == _type || Note == _note || Setting == _setting)
            {
                Column = _column;
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Check(string _content, out string _msg)
        {
            bool flag = true;
            _msg = "";

            // 检查是否重复
            if (IsDifferent)
            {
                if (differentContents.Contains(_content))
                {
                    _msg = "当前属性设置标记为isDifferent, 属性不可重复。该位置属性已重复。";
                    flag = false;
                }
                else
                {
                    differentContents.Add(_content);
                }
            }

            // 检查格式
            if (flag)
            {
                if (IsArray)
                {
                    // 数组可以为空，不检查
                    if (_content != "")
                    {
                        string[] contentArr = _content.Split(data.PropertyArrSplitChar);
                        for (int i = 0; i < contentArr.Length; i++)
                        {
                            if (!OnFormatCheck(contentArr[i]))
                            {
                                flag = false;
                            }
                        }
                    }
                }
                else
                {
                    flag = OnFormatCheck(_content);
                }

                if (!flag)
                {
                    _msg = "此数据并不是该类型，转换失败。";
                }
            }

            return flag;
        }

        public string EmptyReplace()
        {
            if (IsArray)
            {
                return "";
            }
            else
            {
                return OnEmptyReplace();
            }
        }

        public bool HasSetting(PropertySetting _setting)
        {
            return System.Array.IndexOf(PropertySettings, _setting) >= 0;
        }

        /// <summary>
        /// 格式检查，检查格式是否符合要求
        /// </summary>
        /// <param name="_content">属性内容</param>
        /// <returns>格式是否符合要求</returns>
        protected abstract bool OnFormatCheck(string _content);

        /// <summary>
        /// 格式检查，检查格式是否符合要求
        /// </summary>
        /// <returns></returns>
        /// <summary>
        /// 当为表格为空时，替换为的字符串
        /// </summary>
        /// <returns>替换字符串</returns>
        public abstract string OnEmptyReplace();

        /// <summary>
        /// 从字符串中转化得到该属性的函数
        /// </summary>
        /// <returns>Parse函数字符串</returns>
        public abstract string GetParseFunc();

        /// <summary>
        /// 从字符串中转化得到该属性数组的函数
        /// </summary>
        /// <returns>数组Parse函数字符串</returns>
        public abstract string GetArrParseFunc();
    }
}